package com.cungmua.app.base;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.ViewStub;

import com.cungmua.app.R;
import com.cungmua.app.activity.MenuListFragment;
import com.cungmua.app.model.Category;
import com.cungmua.app.model.City;
import com.cungmua.app.model.User;
import com.cungmua.app.model.request.LoginRequest;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/18/14.
 */
public  class BaseDrawerActivitys extends BaseAuthenticatedActivity{
    private int mTitleRes;
    protected ListFragment mFrag;
    SlidingMenu sm;

    List<Category> categoryList=new ArrayList<Category>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.title_activity_session_details);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);

        if (savedInstanceState == null) {
            FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();

//            drawer menu
            prepareListData();

            mFrag = new MenuListFragment(categoryList);
            t.replace(R.id.menu_frame, mFrag);
            t.commit();

        } else {
            mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
        }

        // customize the SlidingMenu
        sm = getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.LEFT);



    }

    public  void showMenu(){
        sm.showMenu();
    }

    public void hideMenu(){
      //  sm.setSlidingEnabled(false);
    }
    private void prepareListData() {

        categoryList = getCategoryList();
        for (int i = 0; i < categoryList.size(); i++) {
            switch (categoryList.get(i).getId()){
                case 56:
                    categoryList.get(i).setIconLeft(ICON_DRAW_FASHION);
                    break;
                case 179:
                    categoryList.get(i).setIconLeft(ICON_DRAW_FASHION_ACCESSORIES);
                    break;
                case 53:
                    categoryList.get(i).setIconLeft(ICON_DRAW_FOOD_RESTAURANT);
                    break;
                case 54:
                    categoryList.get(i).setIconLeft(ICON_DRAW_HEALTH_BEAUTY);
                    break;
                case 52:
                    categoryList.get(i).setIconLeft(ICON_DRAW_TRAVEL_RESORT);
                    break;
                case 55:
                    categoryList.get(i).setIconLeft(ICON_DRAW_ENTERTAIMENT_EDUCATION);
                    break;
                case 57:
                    categoryList.get(i).setIconLeft(ICON_DRAW_HOUSEHOLD_FURNITURE);
                    break;
                case 59:
                    categoryList.get(i).setIconLeft(ICON_DRAW_ELECTRONIC_TECHNOLOGY);
                    break;
                case 68:
                    categoryList.get(i).setIconLeft(ICON_DRAW_MOTHER_BABY);
                    break;
            }
        }

        Category cate= new Category();
        cate.setId(HOME);
        cate.setName(getResources().getString(R.string.main_pages));
        cate.setIconLeft(R.drawable.icon_draw_home);
        categoryList.add(0,cate);

        Category cate_promotion= new Category();
        cate_promotion.setId(PROMOTION);
        cate_promotion.setName(getResources().getString(R.string.promotions));
        cate_promotion.setIconLeft(R.drawable.icon_draw_home_promotions);
        categoryList.add(1,cate_promotion);

        Category cate_danhmuc= new Category();
        cate_danhmuc.setId(DANHMUC);
        cate_danhmuc.setName(getResources().getString(R.string.danhmuc));
        categoryList.add(2,cate_danhmuc);


        Category cate_thongtin= new Category();
        cate_thongtin.setId(THONGTIN);
        cate_thongtin.setName(getResources().getString(R.string.info));
        categoryList.add(12,cate_thongtin);

        Category cate_thongtin_taikhoan= new Category();
        cate_thongtin_taikhoan.setId(THONGTIN_TAIKHOAN);
        cate_thongtin_taikhoan.setName(getResources().getString(R.string.draw_profile));
        cate_thongtin_taikhoan.setIconLeft(R.drawable.icon__draw_login);
        prepareUser(cate_thongtin_taikhoan);

        categoryList.add(13,cate_thongtin_taikhoan);

        Category cate_thongtin_huuich= new Category();
        cate_thongtin_huuich.setId(THONGTIN_HUUICH);
        cate_thongtin_huuich.setChildren(prepareInfoAdvances());
        cate_thongtin_huuich.setName(getResources().getString(R.string.draw_profile_huu_ich));
        cate_thongtin_huuich.setIconLeft(R.drawable.icon__draw_register);
        categoryList.add(14,cate_thongtin_huuich);


        Category cate_choose_city= new Category();
        cate_choose_city.setId(SELECT_CITY);
        cate_choose_city.setName(getResources().getString(R.string.city));
        categoryList.add(15,cate_choose_city);

        Category cate_hochiminh= new Category();

        cate_hochiminh.setId(DEFAULT_CITY);
        cate_hochiminh.setIconLeft(R.drawable.cm_position);
        String cityDefault=getCurrentCity().getName()!=null?getCurrentCity().getName():getResources().getString(R.string.city_default);
        cate_hochiminh.setName(cityDefault);

        cate_hochiminh.setChildren(prepareLocation());
        categoryList.add(16,cate_hochiminh);

        if(getCurrentUser().getId()==0){
            Category cate_login= new Category();
            cate_login.setId(LOGIN);
            cate_login.setName(getResources().getString(R.string.dangnhap));
            cate_login.setIconLeft(R.drawable.icon__draw_login);
            categoryList.add(17,cate_login);
        }

    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        setContentView(R.layout.drawer_container);
        ViewStub stub = (ViewStub) findViewById(R.id.stub_main_content);
        stub.setLayoutResource(getLayoutResource());
        stub.inflate();

        loadControls(savedInstanceState);
        updateView();

        if (getCurrentCity() == null) {
            goToChooseCity();
        }
    }

    @Override
    public void updateView() {

    }

    @Override
    public void loadControls(Bundle savedInstanceState) {

    }

    @Override
    public void setLastLoginRequest(LoginRequest request) {

    }

    @Override
    public void setCurrentUser(User data) {

    }


    private List<Category> prepareLocation() {
        List<Category> childrens = new ArrayList<Category>();
        List<City> cityList = getCityList();

        City currentCity = getCurrentCity();


        for (int i = 0; i < cityList.size(); i++) {
            Category category = new Category(cityList.get(i).getId(), cityList.get(i).getName(), DEFAULT_CITY,R.drawable.cm_position);
            childrens.add(category);
        }
        return  childrens;
    }

    private void prepareUser(Category cate_thongtin_taikhoan) {
            List<Category> userInfo=new ArrayList<Category>();
        if (getCurrentUser().getId() == 0) { // default user
            Category cate_danghap=new Category(THONGTIN_TAIKHOAN_DANGNHAP,getResources().getString(R.string.info_dangnhap),THONGTIN_TAIKHOAN,R.drawable.icon__draw_login);
            Category cate_dangky=new Category(THONGTIN_TAIKHOAN_DANGKY,getResources().getString(R.string.info_dangky),THONGTIN_TAIKHOAN,R.drawable.icon__draw_register);

            userInfo.add(cate_danghap);
            userInfo.add(cate_dangky);



        }else{
            Category logined_thongtin_taikhoan=new Category(LOGINED_THONGTIN_TAIKHOAN,getString(R.string.draw_profile),THONGTIN_HUUICH,R.drawable.icon__draw_login);
            Category logined_thongtin_donhang=new Category(LOGINED_THONGTIN_DONHANG,getResources().getString(R.string.info_donhang),THONGTIN_HUUICH,R.drawable.icon__draw_login);
            Category logined_thongtin_tienthuong=new Category(LOGINED_THONGTIN_TIENTHUONG,getString(R.string.award_money),THONGTIN_HUUICH,R.drawable.icon_draw_money);
            Category logined_thongtin_taikhoan_premium=new Category(LOGINED_THONGTIN_TAIKHOAN_PREMIUM,getString(R.string.taikhoan_premium),THONGTIN_HUUICH,R.drawable.icon__draw_register);
            Category logined_thongtin_doimatkhau=new Category(LOGINED_THONGTIN_DOIMATKHAU,getString(R.string.change_password),THONGTIN_HUUICH,R.drawable.icon__draw_register);
            Category logined_thongtin_dangxuat=new Category(LOGINED_THONGTIN_DANGXUAT,getString(R.string.logout),THONGTIN_HUUICH,R.drawable.icon_draw_logout);

            userInfo.add(logined_thongtin_taikhoan);
            userInfo.add(logined_thongtin_donhang);
            userInfo.add(logined_thongtin_tienthuong);
            userInfo.add(logined_thongtin_taikhoan_premium);
            userInfo.add(logined_thongtin_doimatkhau);
            userInfo.add(logined_thongtin_dangxuat);
        }


           cate_thongtin_taikhoan.setChildren(userInfo);
    }
    private List<Category> prepareInfoAdvances(){
        List<Category> infoAdvances=new ArrayList<Category>();
        Category huu_ich_about=new Category(THONGTIN_HUUICH_ABOUT,getString(R.string.label_menu_info_advance_about),THONGTIN_TAIKHOAN,R.drawable.dot);
        Category huu_ich_quy_che=new Category(THONGTIN_HUUICH_QUYCHE,getString(R.string.label_menu_info_advance_quyche),THONGTIN_TAIKHOAN,R.drawable.dot);
        Category huu_ich_doi_tra=new Category(THONGTIN_HUUICH_TRAHANG,getString(R.string.label_menu_info_advance_trahang),THONGTIN_TAIKHOAN,R.drawable.dot);
        Category huu_ich_huong_dan_mua=new Category(THONGTIN_HUUICH_HUONGDAN,getString(R.string.label_menu_info_advance_huongdanmua),THONGTIN_TAIKHOAN,R.drawable.dot);
        Category huu_ich_hinh_thuc_thanh_toan=new Category(THONGTIN_HUUICH_HINHTHUC,getString(R.string.label_menu_info_advance_hinhthuc_thanhtoan),THONGTIN_TAIKHOAN,R.drawable.dot);
        Category huu_ich_hinh_thuc_giao=new Category(THONGTIN_HUUICH_HINHTHUC_GIAO,getString(R.string.label_menu_info_advance_hinhthuc_giao),THONGTIN_TAIKHOAN,R.drawable.dot);

        infoAdvances.add(huu_ich_about);
        infoAdvances.add(huu_ich_quy_che);
        infoAdvances.add(huu_ich_doi_tra);
        infoAdvances.add(huu_ich_huong_dan_mua);
        infoAdvances.add(huu_ich_hinh_thuc_thanh_toan);
        infoAdvances.add(huu_ich_hinh_thuc_giao);

        return  infoAdvances;
    }
}
